﻿Public Class FormYZZH
    Private WithEvents UcDigitalKeyboard1 As ucDigitalKeyboard
    Dim objUser As New clsUser
    Dim objWorkshopCurrent As New clsWorkshopCurrent
    Dim objPgdMes As New clsPgdMes
    Dim objProdSpec As New clsProdSpec
    Dim objProdInfo As New clsProdInfo
    Dim objGJBinfo As New clsGJBinfo
    Dim objFLBinfo As New clsFLBinfo
    Dim objJpWtMaster As New clsJpWtMaster
    Dim objJpMst As New clsJPMst
    Dim rb_checked_text As String = "3,2"
    Dim rb_checked_details_text As String = "6"
    Dim current_section As String '当前工序
    Dim dtjpd As New DataTable
    Private Sub FormYZZH_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized
        dtjpd.Columns.Add("model")
        dtjpd.Columns.Add("material_code")
        connectDB()
        current_section = GetIniFile("SECTION", "current_section", "", g_szInitFileName)
        RadioButton4.Checked = True
        txt_tray_no.Focus()
        initForm()
        setDgvData()
    End Sub
    Private Sub connectDB()
        g_szInitFileName = My.Application.Info.DirectoryPath & "\Client_YZZH.ini"

        g_Server.szDrive = GetIniFile("SERVER", "DRIVE", "PostgreSQL ANSI", g_szInitFileName)
        g_Server.szIPadr = GetIniFile("SERVER", "IP", "", g_szInitFileName)
        g_Server.szDbn = GetIniFile("SERVER", "DBN", "ZHENANSF", g_szInitFileName)
        g_Server.szUid = GetIniFile("SERVER", "UID", "sa", g_szInitFileName)
        g_Server.szPas = GetIniFile("SERVER", "PAS", "sa", g_szInitFileName)
        g_Server.iOver = CInt(GetIniFile("SERVER", "OVER", "-1", g_szInitFileName))

        If False = g_fDBconnect And 0 = g_iDBconnResult Then
            If "" = g_Server.szIPadr Or "0.0.0.0" = g_Server.szIPadr Then
            Else
                g_fDBconnect = True

                'データベース接続
                With g_Server
                    g_iDBconnResult = DBacs.Connect(.szDrive, .szIPadr, .szDbn, .szUid, .szPas, .iOver)
                    If 1 <> g_iDBconnResult Then
                        .szDrive = CStr(IIf("PostgreSQL" = .szDrive, "PostgreSQL ANSI", "PostgreSQL"))
                        g_iDBconnResult = DBacs.Connect(.szDrive, .szIPadr, .szDbn, .szUid, .szPas, .iOver)
                    End If
                End With
            End If
        End If

    End Sub
    Private Sub getUserListByCJ()
        Dim dtUser As DataTable = objUser.getUserListByCJ(rb_checked_text, rb_checked_details_text)
        cmbOpers.Properties.Items.Clear()
        For index = 0 To dtUser.Rows.Count - 1
            cmbOpers.Properties.Items.Add(dtUser.Rows(index)("user_name"))
        Next
        cmbOpers.Properties.Items(0).CheckState = CheckState.Checked
        cmbOpers.Properties.DropDownRows = cmbOpers.Properties.Items.Count + 1
        cmbOpers.Properties.ShowButtons = False
        cmbOpers.Properties.ShowPopupCloseButton = False
    End Sub
    Private Sub btnRefresh_Click(sender As Object, e As EventArgs) Handles btnRefresh.Click
        initForm()
        setDgvData()
    End Sub

    Public Sub setDgvData()
        Dim dtProdInfo As DataTable = objProdInfo.getProdInfoListByStatus("1")
        dgv.AutoGenerateColumns = False
        dgv.DataSource = dtProdInfo
    End Sub

    Private Sub getRbChechedText()
        If RadioButton4.Checked Then
            rb_checked_text = "3,2"
            rb_checked_details_text = "6"
        End If
        If RadioButton5.Checked Then
            rb_checked_text = "3,2"
            rb_checked_details_text = "8"
        End If
        If RadioButton6.Checked Then
            rb_checked_text = "3,2"
            rb_checked_details_text = "10"
        End If
        If RadioButton1.Checked Then
            rb_checked_text = "2"
            rb_checked_details_text = "1"
        End If
    End Sub

    Private Sub RadioButtonText_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton4.CheckedChanged, RadioButton5.CheckedChanged, RadioButton6.CheckedChanged, RadioButton1.CheckedChanged
        getRbChechedText()
        getUserListByCJ()
    End Sub

    Public Sub initForm()
        Dim dt As DataTable = objWorkshopCurrent.GetListByWorkShopIdAndCurrentSection("0", current_section)
        cmb_support_model.DisplayMember = "support_model"
        cmb_support_model.ValueMember = "b_pgd_sys_id"
        cmb_support_model.DataSource = dt
        If dt.Rows.Count > 0 Then
            If String.IsNullOrEmpty(cmb_support_model.Text) = False Then
                cmb_support_model.SelectedIndex = 0
                Dim dtPgdMes As DataTable = objPgdMes.getList(cmb_support_model.SelectedValue.ToString())
                txt_mes_project_code.Text = dtPgdMes.Rows(0)("mes_project_code").ToString()
                txt_mes_project_name.Text = dtPgdMes.Rows(0)("mes_project_name").ToString()
                lblProdSpecName.Text = objProdSpec.getSpecNameByID(cmb_support_model.Text)
                Dim dtPS As DataTable = objProdSpec.selectAllbySpecId(cmb_support_model.Text)
                If dtPS.Rows.Count > 0 Then
                    txtFlbType.Text = dtPS.Rows(0)("flb_type").ToString()
                    txtFlbType.Tag = dtPS.Rows(0)("flb_material_code").ToString()
                    lblGJBXH.Text = dtPS.Rows(0)("gjb_type").ToString()
                    lblGJBXH.Tag = dtPS.Rows(0)("gjb_material_code").ToString()
                    lblJPZL.Text = dtPS.Rows(0)("rubber_layer_thickness_total").ToString()
                    lblGJBZL.Text = dtPS.Rows(0)("gjb_st_wt").ToString()
                    lblJPPS.Text = dtPS.Rows(0)("jp_layer_num").ToString()
                    lblGJBPS.Text = dtPS.Rows(0)("gjb_layer_num").ToString()
                    lblFLBXH.Text = dtPS.Rows(0)("flb_type").ToString()
                    lblFLBXH.Tag = dtPS.Rows(0)("flb_material_code").ToString()
                    'Dim dtMJ As DataTable = objJpMst.getMstJpByMaterialCode(dtPgdMes.Rows(0)("jp_type").ToString())
                    Dim dtMJ As DataTable = objJpMst.getMstJpByMaterialCode(dtPgdMes.Rows(0)("material_code").ToString())
                    lblJLXH.Text = dtMJ.Rows(0)("model").ToString()
                    Dim dtjp As String = dtPS.Rows(0)("jp_material_code").ToString()
                    Dim dtjps As String() = dtjp.Split("/")
                    Dim dtjpp As String = dtPS.Rows(0)("jp_type").ToString()
                    Dim dtjpps As String() = dtjpp.Split("/")
                    dtjpd.Rows.Clear()
                    For index = 0 To dtjps.Count() - 1
                        dtjpd.Rows.Add(dtjpps(index), dtjps(index))
                    Next
                    cmbJPXH_SJ.DisplayMember = "model"
                    cmbJPXH_SJ.ValueMember = "material_code"
                    cmbJPXH_SJ.DataSource = dtjpd
                    If dtjpd.Rows.Count > 0 Then
                        If DBNull.Value.Equals(dtPgdMes.Rows(0)("jp_type")) Then
                            cmbJPXH_SJ.SelectedIndex = 0
                        Else
                            cmbJPXH_SJ.SelectedValue = dtPgdMes.Rows(0)("jp_type")
                        End If
                    End If
                    lblJLXH.Text = cmbJPXH_SJ.Text.Trim()
                End If
            Else
                txt_mes_project_code.Text = String.Empty
                txt_mes_project_name.Text = String.Empty
                lblProdSpecName.Text = String.Empty
                txtFlbType.Text = String.Empty
                lblJLXH.Text = String.Empty
                lblGJBXH.Text = "0"
                lblJPZL.Text = "0"
                lblGJBZL.Text = "0"
                lblJPPS.Text = "0"
                lblGJBPS.Text = "0"
                lblFLBXH.Text = "0"
            End If
        End If
    End Sub

    Private Sub cmbJPXH_SJ_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbJPXH_SJ.SelectedIndexChanged
        lblJLXH.Text = cmbJPXH_SJ.Text.Trim()
    End Sub

    Private Function check() As Boolean
        Dim rtn As Boolean = True
        If lblFLBXH.Tag <> txt_flb_type.Tag Then
            MsgBox("法兰板型号不符！")
            Return False
        End If
        'If lblGJBXH.Tag <> lblGJBXH_SJ.Tag Then
        '    MsgBox("骨架板型号不符！")
        '    Return False
        'End If
        If lblGJBPS.Text <> txtGJBPS_SJ.Text Then
            MsgBox("骨架板片数不符！")
            Return False
        End If
        If CDbl(txtGJBZL_SJ.Text) <= 0 Then
            MsgBox("骨架板重量需 ＞ 0！")
            Return False
        End If
        If lblJPPS.Text <> txtJP_Piece_num.Text Then
            MsgBox("胶片片数不符！")
            Return False
        End If
        If txtJP_ZL.Value <= 0 Then
            MsgBox("胶片重量需 ＞ 0！")
            Return False
        End If
        If String.IsNullOrEmpty(txtBarCodeJp.Text.Trim()) = False Then
            If lblJLXH.Text.Trim().Equals(lblJPXH_SJ.Text.Trim()) = False Then
                MsgBox("胶片型号不符！")
                Return False
            End If
        End If
        Return rtn
    End Function
    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        initForm()
        If check() Then
            Dim rtn As Integer = objProdInfo.updateProdInfo(lblprodid.Text.Trim(), lblgjb_sys_id.Text.Trim(), txtGJBDLNo.Text.Trim(), txtBarCodeJp.Text.Trim(), txtJP_Piece_num.Text, txtJP_ZL.Text.Trim(), lblJLXH.Text.Trim(), cmbOpers.EditValue.ToString())
            If rtn <> -2 Then
                Clear()
                initForm()
                setDgvData()
                MsgBox("保存成功")
            Else
                MsgBox("保存失败")
            End If
        End If
    End Sub
    Private Sub Clear()
        lblprodid.Text = String.Empty
        txt_tray_no.Text = String.Empty
        txt_flb_type.Text = String.Empty
        txtGJBDLNo.Text = String.Empty
        lblGJBXH_SJ.Text = String.Empty
        txtGJBPS_SJ.Text = "0"
        lblLabelNo.Text = String.Empty
        txtGJBZL_SJ.Text = "0.0"
        lblgjb_sys_id.Text = String.Empty
        txtBarCodeJp.Text = String.Empty
        lblJPXH_SJ.Text = String.Empty
        txtJP_Piece_num.Value = 0
        txtJP_ZL.Value = 0
    End Sub
    Private Sub txtGJBDLNo_KeyDown(sender As Object, e As KeyEventArgs) Handles txtGJBDLNo.KeyDown
        If String.IsNullOrEmpty(txtGJBDLNo.Text.Trim()) = False Then
            If e.KeyCode = Keys.Enter Then
                'txtGJBDLNo.Text.Remove(txtGJBDLNo.Text.Length - 1)
                txtGJBDLNo.Text = txtGJBDLNo.Text.ToUpper()
                Dim dt As DataTable = objGJBinfo.getListByDlNo(txtGJBDLNo.Text.Trim())
                If dt.Rows.Count > 0 Then
                    txtGJBPS_SJ.Text = dt.Rows(0)("layer_num")
                    txtGJBZL_SJ.Text = dt.Rows(0)("wt")
                    lblGJBXH_SJ.Text = dt.Rows(0)("gjb_type")
                    lblGJBXH_SJ.Tag = dt.Rows(0)("material_code")
                    lblgjb_sys_id.Text = dt.Rows(0)("sys_id")
                    If dt.Rows(0)("label_no").Equals(DBNull.Value) = False Then
                        lblLabelNo.Text = dt.Rows(0)("label_no")
                        lblLabelNo.Text = String.Format("{0}号标签", lblLabelNo.Text)
                    End If
                    'Qwe123
                Else
                    txtGJBPS_SJ.Text = "0"
                    txtGJBZL_SJ.Text = "0.0"
                    lblGJBXH_SJ.Text = String.Empty
                    lblgjb_sys_id.Text = String.Empty
                    lblLabelNo.Text = String.Empty
                    MsgBox("未查询到未使用状态下的此骨架板标签号，请核查！", vbYes, "提示")
                End If
            End If
        End If

    End Sub

    Private Sub txtBarCodeJp_KeyDown(sender As Object, e As KeyEventArgs) Handles txtBarCodeJp.KeyDown
        If String.IsNullOrEmpty(txtBarCodeJp.Text.Trim()) = False Then
            If e.KeyCode = Keys.Enter Then
                Dim dt As DataTable = objJpWtMaster.getListByBarcode(txtBarCodeJp.Text.Trim())
                If dt.Rows.Count > 0 Then
                    lblJPXH_SJ.Text = dt.Rows(0)("model")
                    txtJP_Piece_num.Text = dt.Rows(0)("pieces_num")
                    txtJP_ZL.Text = dt.Rows(0)("weight")
                Else
                    lblJPXH_SJ.Text = String.Empty
                    txtJP_Piece_num.Text = "0"
                    txtJP_ZL.Text = "0.0"
                    MsgBox("未查询到未使用状态下的此胶片标签号，请核查！", vbYes, "提示")
                End If
            End If
        End If
    End Sub

    Private Sub txt_tray_no_KeyDown(sender As Object, e As KeyEventArgs) Handles txt_tray_no.KeyDown
        If String.IsNullOrEmpty(txt_tray_no.Text.Trim()) = False Then
            If e.KeyCode = Keys.Enter Then
                txt_tray_no.Text = txt_tray_no.Text.ToUpper()
                Dim dtPI As DataTable = objProdInfo.getProdInfoListByTrayNoAndStatus(txt_tray_no.Text.Trim(), "0")
                If dtPI.Rows.Count > 0 Then
                    lblprodid.Text = dtPI.Rows(0)("prod_id").ToString()
                    Dim dt As DataTable = objFLBinfo.getFlbInfoBySysId(dtPI.Rows(0)("flb_sys_id").ToString())
                    If dt.Rows.Count > 0 Then
                        txt_flb_type.Text = dt.Rows(0)("flb_type").ToString()
                        If DBNull.Value.Equals(dt.Rows(0)("material_code")) Then
                            txt_flb_type.Tag = dt.Rows(0)("flb_material_code")
                        Else
                            txt_flb_type.Tag = dt.Rows(0)("material_code")
                        End If
                    Else
                        txt_flb_type.Text = String.Empty
                    End If
                Else
                    MsgBox("未查询到此托盘的预装芯座信息，请核查！", vbYes, "提示")
                End If
            End If
        End If
    End Sub

    Private Sub txtNumber_MouseDown(sender As Object, e As MouseEventArgs) Handles txtJP_Piece_num.MouseDown, txtJP_ZL.MouseDown, txt_jp_wt.MouseDown, txtGJBZL_SJ.MouseDown, txtGJBPS_SJ.MouseDown
        sender.Focus
        UcDigitalKeyboard1 = New ucDigitalKeyboard

        If sender.MaskBox.Mask.EditMask = "d" Then
            UcDigitalKeyboard1.B_01 = False
        Else
            UcDigitalKeyboard1.B_01 = True
        End If

        If sender.GetType().Name.Equals("TextEdit") Then
            UcDigitalKeyboard1.B_02 = True
        Else
            UcDigitalKeyboard1.B_02 = False
        End If

        UcDigitalKeyboard1.Txt = sender

        Me.Controls.Add(UcDigitalKeyboard1)
        UcDigitalKeyboard1.Show()
        UcDigitalKeyboard1.BringToFront()
        UcDigitalKeyboard1.Focus()

    End Sub

    Private Sub btnSet_Click(sender As Object, e As EventArgs) Handles btnSet.Click
        If String.IsNullOrEmpty(cmbOpers.EditValue.ToString()) = False Then
            Dim formPlanMes As New FormPlanMes()
            formPlanMes.origin = "FormYZZH"
            formPlanMes.usr = cmbOpers.EditValue.ToString()
            formPlanMes.ShowDialog(Me)
        Else
            MsgBox("请选择操作用户！")
        End If

    End Sub

    Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
        Dim rtn As Integer = objWorkshopCurrent.updateDataClear("0", current_section)
        If rtn = -2 Then
            MsgBox(btnClear.Text + "失败")
        Else
            MsgBox(btnClear.Text + "成功")
            initForm()
        End If
    End Sub

    Private Sub dgv_SelectionChanged(sender As Object, e As EventArgs) Handles dgv.SelectionChanged
        If dgv.SelectedRows.Count > 0 Then
            txt_jp_wt.Value = dgv.SelectedRows(0).Cells("jp_all_wt").Value.ToString()
        End If
    End Sub

    Private Sub btnSaveJpAllWt_Click(sender As Object, e As EventArgs) Handles btnSaveJpAllWt.Click
        If dgv.SelectedRows.Count > 0 Then
            Dim rtn As Integer = objProdInfo.updateProdInfoJpAllWt(dgv.SelectedRows(0).Cells("prod_id").Value.ToString(), txt_jp_wt.Text.Trim())
            If rtn <> -2 Then
                setDgvData()
                MsgBox("保存成功")
            Else
                MsgBox("保存失败")
            End If
        End If
    End Sub

    Private Sub txtGJBDLNo_TextChanged(sender As Object, e As EventArgs) Handles txtGJBDLNo.TextChanged
        If String.IsNullOrEmpty(txtGJBDLNo.Text.Trim()) = False And txtGJBDLNo.Text.Length = 6 Then
            'txtGJBDLNo.Text.Remove(txtGJBDLNo.Text.Length - 1)
            txtGJBDLNo.Text = txtGJBDLNo.Text.ToUpper()
            Dim dt As DataTable = objGJBinfo.getListByDlNo(txtGJBDLNo.Text.Trim())
            If dt.Rows.Count > 0 Then
                txtGJBPS_SJ.Text = dt.Rows(0)("layer_num")
                txtGJBZL_SJ.Text = dt.Rows(0)("wt")
                lblGJBXH_SJ.Text = dt.Rows(0)("gjb_type")
                lblGJBXH_SJ.Tag = dt.Rows(0)("material_code")
                lblgjb_sys_id.Text = dt.Rows(0)("sys_id")
                If dt.Rows(0)("label_no").Equals(DBNull.Value) = False Then
                    lblLabelNo.Text = dt.Rows(0)("label_no")
                    lblLabelNo.Text = String.Format("{0}号标签", lblLabelNo.Text)
                End If
            Else
                txtGJBPS_SJ.Text = "0"
                txtGJBZL_SJ.Text = "0.0"
                lblGJBXH_SJ.Text = String.Empty
                lblgjb_sys_id.Text = String.Empty
                lblLabelNo.Text = String.Empty
            End If

        End If
    End Sub

    Private Sub txtBarCodeJp_TextChanged(sender As Object, e As EventArgs) Handles txtBarCodeJp.TextChanged
        If String.IsNullOrEmpty(txtBarCodeJp.Text.Trim()) = False Then
            Dim dt As DataTable = objJpWtMaster.getListByBarcode(txtBarCodeJp.Text.Trim())
            If dt.Rows.Count > 0 Then
                lblJPXH_SJ.Text = dt.Rows(0)("model")
                txtJP_Piece_num.Text = dt.Rows(0)("pieces_num")
                txtJP_ZL.Text = dt.Rows(0)("weight")
            Else
                lblJPXH_SJ.Text = String.Empty
                txtJP_Piece_num.Text = "0"
                txtJP_ZL.Text = "0.0"
            End If
        End If
    End Sub

    Private Sub btnSetProject_Click(sender As Object, e As EventArgs) Handles btnSetProject.Click
        If String.IsNullOrEmpty(cmbOpers.EditValue.ToString()) = False Then
            Dim formMes As New FormMes()
            formMes.ShowDialog(Me)
        Else
            MsgBox("请选择操作用户！")
        End If
    End Sub

    Private Sub FormMain_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
        DBacs.Disconnect()
    End Sub

End Class